---
description:
  Nest (Nest JS) is a progressive Node.js framework for building efficient, reliable and scalable
  server-side applications.
---

import { Callout } from '@theguild/components'

# Integration with NestJS

[Nest (Nest JS)](https://nestjs.com) is a progressive Node.js framework for building efficient,
reliable and scalable server-side applications.

GraphQL Yoga provides its own Nest GraphQL Driver that support building standalone GraphQL APIs and
[Apollo Federation GraphQL APIs](https://the-guild.dev/graphql/hive/federation) (Gateway and
Services).

<Callout>
  For the setup of a new Nest project, please make sure to read the [Nest GraphQL
  documentation](https://docs.nestjs.com/graphql/quick-start).
</Callout>

## Standalone

### Install

```sh npm2yarn
npm i @nestjs/graphql @graphql-yoga/nestjs graphql-yoga graphql
```

### Create Application Module

```typescript
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs'
import { Module } from '@nestjs/common'
import { GraphQLModule } from '@nestjs/graphql'

@Module({
  imports: [
    GraphQLModule.forRoot<YogaDriverConfig>({
      driver: YogaDriver
    })
  ]
})
export class AppModule {}
```

### Develop GraphQL

This is just a HTTP transport driver; meaning, everything else should work as
[showcased in NestJS documentation](https://docs.nestjs.com/graphql/resolvers).

## Apollo Federation

Separately, we offer a `@graphql-yoga/nestjs-federation` driver which allows building Apollo
Federation Gateways and Services through the `YogaGatewayDriver` and `YogaFederationDriver` drivers.

### Install

```sh npm2yarn
npm i @nestjs/graphql @graphql-yoga/nestjs-federation graphql-yoga graphql
```

#### Subgraph

For the subgraph we use the `YogaFederationDriver` and `YogaFederationDriverConfig`.

```typescript
import { YogaFederationDriver, YogaFederationDriverConfig } from '@graphql-yoga/nestjs-federation'
import { Module } from '@nestjs/common'
import { GraphQLModule } from '@nestjs/graphql'
import { DeprecatedProductsResolver } from './deprecated-products.resolver'
import { InventoryResolver } from './inventory.resolver'
import { ProductResearchResolver } from './product-research.resolver'
import { ProductsResolver } from './products.resolver'
import { UsersResolver } from './users.resolver'

@Module({
  imports: [
    GraphQLModule.forRoot<YogaFederationDriverConfig>({
      driver: YogaFederationDriver,
      typePaths: ['**/*.graphql']
    })
  ],
  providers: [
    UsersResolver,
    ProductsResolver,
    ProductResearchResolver,
    DeprecatedProductsResolver,
    InventoryResolver
  ]
})
export class AppModule {}
```

<Callout>
  A complete example of the subgraph implementation [is available in the
  repository](https://github.com/graphql-hive/graphql-yoga/tree/main/examples/nestjs-apollo-federation-compatibility).
</Callout>

#### Gateway

For the gateway we use the `YogaGatewayDriver` and `YogaGatewayDriverConfig`.

```typescript
import { YogaGatewayDriver, YogaGatewayDriverConfig } from '@graphql-yoga/nestjs-federation'
import { Module } from '@nestjs/common'
import { GraphQLModule } from '@nestjs/graphql'

@Module({
  imports: [
    GraphQLModule.forRoot<YogaGatewayDriverConfig>({
      driver: YogaGatewayDriver,
      gateway: {
        services: [{ name: 'subgraph', url: 'http://subgraph/graphql' }]
      }
    })
  ]
})
export class AppModule {}
```

### Further development

Yoga offers just a federation and gateway driver; meaning, everything else works as
[showcased in NestJS federation documentation](https://docs.nestjs.com/graphql/federation).
